<?php
namespace Kuoqi\Html;

class FormInput{
    public $classname = '';
    public $format = 'input';
    public $dataList = array();
    public $name = '';
    public $value = '';
    public $otps = '';

    public function __construct($cfgs){
        $this->classname = isset($cfgs['classname'])?$cfgs['classname']:'';
        $this->format = isset($cfgs['format'])?$cfgs['format']:'';
        $this->dataList = isset($cfgs['data'])?$cfgs['data']:array();
        $this->name = isset($cfgs['name'])?$cfgs['name']:'';
        $this->value = isset($cfgs['value'])?$cfgs['value']:'';
    }

    public function format(){
        $this->otps = '';
        switch($this->format){
            case 'radio':$this->doRedio();break;
            default;
        }
        return $this->otps;
    }

    //radio
    public function doRedio(){
        $itm = '';
        foreach($this->dataList as $item){
            $isValid = false;
            if($item['value'] == $this->value){
                $isValid = true;
            }
            $itm .= sprintf('<label class="%s%s"><span>%s</span><input type="radio" name="%s" value="%s" %s /></label>'
                ,$this->classname
                ,($isValid?' active':'')
                ,$item['name']
                ,$this->name
                ,$item['value']
                ,$isValid?'checked="checked"':''
            );
        }
        $this->otps = $itm;
    }

}